package constellation.Activity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.R;
import com.example.myapplication.fragment.fragment2;
import com.google.gson.Gson;

import constellation.fragment.luckFragment;
import constellation.fragment.meFragment;
import constellation.fragment.parnterFragment;
import constellation.fragment.starFragment;
import constellation.util.AssetsUtil;
import constellation.util.StartInfo;

public class constellation_Activity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private TextView mainTvTitle;
    private LinearLayout mainLayoutCenter;
    private RadioGroup consteallRg;
    private RadioButton mainRbParnter;
    private RadioButton mainRbLuck;
    private RadioButton mainRbMe;
    private RadioButton mainRbStart;
    starFragment starFragment;
    luckFragment luckFragment;
    meFragment meFragment;
    parnterFragment parnterFragment;
    FragmentManager fragmentManager;
    Fragment nowfragment;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constellation_);
        StartInfo starInfo = initData();//获取Assets文件夹当中的json数据
        /*四个fragment进行初始化*/
        bundle= new Bundle();
        bundle.putSerializable("info",starInfo);
        loadDefaultFragment();
        initView();


    }
public StartInfo initData(){
    String json = AssetsUtil.getJsonByAssets(this, "xzcontent/xzcontent.json");
//        将json字符串转换成创建类的对象
    Gson gson = new Gson();
    StartInfo starInfo = gson.fromJson(json, StartInfo.class);
    //讲assets文件夹当中的相关图片都加载完，存放到内存中
    AssetsUtil.saveLogoBitmapFromAssets(this,starInfo);
    return starInfo;
}
    private void initView() {
        mainTvTitle = (TextView) findViewById(R.id.main_tv_title);
        mainLayoutCenter = (LinearLayout) findViewById(R.id.main_layout_center);
        consteallRg = (RadioGroup) findViewById(R.id.consteall_rg);
        mainRbStart = (RadioButton) findViewById(R.id.main_rb_start);
        mainRbParnter = (RadioButton) findViewById(R.id.main_rb_parnter);
        mainRbLuck = (RadioButton) findViewById(R.id.main_rb_luck);
        mainRbMe = (RadioButton) findViewById(R.id.main_rb_me);
        consteallRg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_rb_start:
                if (starFragment==null){
                    starFragment=new starFragment();
                }
                replaceFragment(starFragment);
                break;
            case R.id.main_rb_parnter:
                if (parnterFragment==null){
                    parnterFragment=new parnterFragment();
                    parnterFragment.setArguments(bundle);
                }

                replaceFragment(parnterFragment);
                break;
            case R.id.main_rb_luck:
                if (luckFragment==null){
                    luckFragment=new luckFragment();
                    luckFragment.setArguments(bundle);
                }

                replaceFragment(luckFragment);
                break;
            case R.id.main_rb_me:
                if (meFragment==null){
                    meFragment=new meFragment();
                    meFragment.setArguments(bundle);
                }
                replaceFragment(meFragment);
                break;
        }

    }
    /*
加载第一个页面
 */
    void loadDefaultFragment() {
//定义管理器
        fragmentManager = getSupportFragmentManager();
//定义事务对象
        FragmentTransaction ft = fragmentManager.beginTransaction();
//添加Fragment
        starFragment = new starFragment();
        starFragment.setArguments(bundle);
        ft.add(R.id.main_layout_center, starFragment);
        nowfragment=starFragment;
        ft.commit();
    }
    void replaceFragment(Fragment fragment){
        fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft=fragmentManager.beginTransaction();
        if (fragment.isAdded()){
            ft.hide(nowfragment).show(fragment);
        }else {
            ft.add(R.id.main_layout_center,fragment).hide(nowfragment);
//            ft.addToBackStack(null);
        }
        nowfragment=fragment;
        ft.commit();
    }
}
